#coding=utf-8
#Boa:FramePanel:PanelProfitMall

import wx

from MyListCtrl import *
from wx.lib.splitter import MultiSplitterWindow

import PanelSkuSearch


[wxID_PANELPROFITMALL, wxID_PANELPROFITMALLBTNALLDATE, 
 wxID_PANELPROFITMALLBTNDAYAFTER, wxID_PANELPROFITMALLBTNDAYBEFORE, 
 wxID_PANELPROFITMALLBTNONEDAY, wxID_PANELPROFITMALLBTNONEMONTH, 
 wxID_PANELPROFITMALLBTNONEWEEK, wxID_PANELPROFITMALLBTNONEYEAR, 
 wxID_PANELPROFITMALLBTNSEARCH, wxID_PANELPROFITMALLCHECKBOX_40, 
 wxID_PANELPROFITMALLCHECKBOX_41, wxID_PANELPROFITMALLDATEEND, 
 wxID_PANELPROFITMALLDATESTART, wxID_PANELPROFITMALLSTATICTEXT1, 
 wxID_PANELPROFITMALLTDATE, 
] = [wx.NewId() for _init_ctrls in range(15)]

class PanelProfitMall(wx.Panel):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Panel.__init__(self, id=wxID_PANELPROFITMALL,
              name=u'PanelProfitMall', parent=prnt, pos=wx.Point(341, 138),
              size=wx.Size(1181, 689), style=wx.TAB_TRAVERSAL)
        self.SetClientSize(wx.Size(1165, 651))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.TDate = wx.StaticText(id=wxID_PANELPROFITMALLTDATE,
              label=u'\u67e5\u8be2\u65e5\u671f\uff1a', name=u'TDate',
              parent=self, pos=wx.Point(22, 38), size=wx.Size(85, 19), style=0)
        self.TDate.SetAutoLayout(True)
        self.TDate.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'Arial'))

        self.btnSearch = wx.Button(id=wxID_PANELPROFITMALLBTNSEARCH,
              label=u'\u67e5\u8be2', name=u'btnSearch', parent=self,
              pos=wx.Point(816, 32), size=wx.Size(72, 29), style=0)
        self.btnSearch.Bind(wx.EVT_BUTTON, self.OnBtnSearchButton,
              id=wxID_PANELPROFITMALLBTNSEARCH)

        self.btnDayAfter = wx.Button(id=wxID_PANELPROFITMALLBTNDAYAFTER,
              label=u'\u540e\u4e00\u5929', name=u'btnDayAfter', parent=self,
              pos=wx.Point(720, 30), size=wx.Size(64, 29), style=0)
        self.btnDayAfter.Bind(wx.EVT_BUTTON, self.OnBtnDayAfterButton,
              id=wxID_PANELPROFITMALLBTNDAYAFTER)

        self.btnDayBefore = wx.Button(id=wxID_PANELPROFITMALLBTNDAYBEFORE,
              label=u'\u524d\u4e00\u5929', name=u'btnDayBefore', parent=self,
              pos=wx.Point(656, 30), size=wx.Size(64, 29), style=0)
        self.btnDayBefore.Bind(wx.EVT_BUTTON, self.OnBtnDayBeforeButton,
              id=wxID_PANELPROFITMALLBTNDAYBEFORE)

        self.btnAllDate = wx.Button(id=wxID_PANELPROFITMALLBTNALLDATE,
              label=u'\u5168\u90e8', name=u'btnAllDate', parent=self,
              pos=wx.Point(594, 30), size=wx.Size(46, 29), style=0)
        self.btnAllDate.Bind(wx.EVT_BUTTON, self.OnBtnAllDateButton,
              id=wxID_PANELPROFITMALLBTNALLDATE)

        self.btnOneYear = wx.Button(id=wxID_PANELPROFITMALLBTNONEYEAR,
              label=u'\u4e00\u5e74', name=u'btnOneYear', parent=self,
              pos=wx.Point(546, 30), size=wx.Size(46, 29), style=0)
        self.btnOneYear.Bind(wx.EVT_BUTTON, self.OnBtnOneYearButton,
              id=wxID_PANELPROFITMALLBTNONEYEAR)

        self.btnOneMonth = wx.Button(id=wxID_PANELPROFITMALLBTNONEMONTH,
              label=u'\u4e00\u6708', name=u'btnOneMonth', parent=self,
              pos=wx.Point(498, 30), size=wx.Size(46, 29), style=0)
        self.btnOneMonth.Bind(wx.EVT_BUTTON, self.OnBtnOneMonthButton,
              id=wxID_PANELPROFITMALLBTNONEMONTH)

        self.btnOneWeek = wx.Button(id=wxID_PANELPROFITMALLBTNONEWEEK,
              label=u'\u4e00\u5468', name=u'btnOneWeek', parent=self,
              pos=wx.Point(450, 30), size=wx.Size(46, 29), style=0)
        self.btnOneWeek.Bind(wx.EVT_BUTTON, self.OnBtnOneWeekButton,
              id=wxID_PANELPROFITMALLBTNONEWEEK)

        self.btnOneDay = wx.Button(id=wxID_PANELPROFITMALLBTNONEDAY,
              label=u'\u5f53\u65e5', name=u'btnOneDay', parent=self,
              pos=wx.Point(402, 30), size=wx.Size(46, 29), style=0)
        self.btnOneDay.Bind(wx.EVT_BUTTON, self.OnBtnOneDayButton,
              id=wxID_PANELPROFITMALLBTNONEDAY)

        self.dateEnd = wx.DatePickerCtrl(id=wxID_PANELPROFITMALLDATEEND,
              name=u'dateEnd', parent=self, pos=wx.Point(278, 30),
              size=wx.Size(116, 27), style=wx.DP_DROPDOWN)

        self.staticText1 = wx.StaticText(id=wxID_PANELPROFITMALLSTATICTEXT1,
              label=u'\u81f3', name='staticText1', parent=self,
              pos=wx.Point(254, 38), size=wx.Size(18, 18), style=0)

        self.dateStart = wx.DatePickerCtrl(id=wxID_PANELPROFITMALLDATESTART,
              name=u'dateStart', parent=self, pos=wx.Point(126, 30),
              size=wx.Size(120, 27), style=wx.DP_DROPDOWN)
        self.dateStart.SetLabel(u'2014-04-24')
        self.dateStart.SetValue(wx.DateTimeFromDMY(24, 3, 2014, 0, 0, 0))
        self.dateStart.SetToolTipString(u'dateStart')

        self.checkBox_40 = wx.CheckBox(id=wxID_PANELPROFITMALLCHECKBOX_40,
              label=u'\u540c\u83b1\u521b', name=u'checkBox_40', parent=self,
              pos=wx.Point(952, 40), size=wx.Size(80, 16), style=0)
        self.checkBox_40.SetValue(True)
        self.checkBox_40.Bind(wx.EVT_CHECKBOX, self.OnCheckBox_40Checkbox,
              id=wxID_PANELPROFITMALLCHECKBOX_40)

        self.checkBox_41 = wx.CheckBox(id=wxID_PANELPROFITMALLCHECKBOX_41,
              label=u'\u6613\u5929\u8fbe', name=u'checkBox_41', parent=self,
              pos=wx.Point(1040, 40), size=wx.Size(72, 16), style=0)
        self.checkBox_41.SetValue(True)
        self.checkBox_41.Bind(wx.EVT_CHECKBOX, self.OnCheckBox_41Checkbox,
              id=wxID_PANELPROFITMALLCHECKBOX_41)

    def __init__(self, parent):
        self._init_ctrls(parent)

        self.myInit()

    def myInit(self):

        # left:category  right:product
        self.splitterWin = MultiSplitterWindow(self, style=wx.SP_LIVE_UPDATE)
        self.categoryList = MyCategoryTreeCtrl(self.splitterWin)

        self.splitterRight = MultiSplitterWindow(self.splitterWin, style=wx.SP_LIVE_UPDATE)
        self.m_sku=((),())
        self.panelSku = PanelSkuSearch.PanelSkuSearch(self.splitterRight, self)
        self.panelSku.set_category(1) # 电焊网
        self.proList = MyListCtrlProductProfitMall(self.splitterRight)

        self.splitterRight.SetOrientation(wx.VERTICAL)
        self.splitterRight.SizeWindows()
        self.splitterRight.AppendWindow(self.panelSku, 100)
        self.splitterRight.AppendWindow(self.proList, 350)

        self.splitterWin.AppendWindow(self.categoryList, 150)
        self.splitterWin.AppendWindow(self.splitterRight, 250)
        
        self.splitterWin.Move(wx.Point(10, 80))
        # 设置大小为什么必须放在最后？放在AppendWindow之前导致sashPos参数不起作用
        self.splitterWin.SetSize(wx.Size(1200, 560))
        self.splitterWin.SetOrientation(wx.HORIZONTAL)
        self.splitterWin.SizeWindows()

        self.categoryList.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeSelChanged)
        
        self.curKeywords = []
        self.curCategory = None

        self.dateStart.SetValue(wx.DateTime.Today())
        self.dateEnd.SetValue(wx.DateTime.Today())

    def OnBtnOneDayButton(self, event):
        self.dateStart.SetValue(wx.DateTime.Today())
        self.dateEnd.SetValue(wx.DateTime.Today())

    def OnBtnOneWeekButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Days(7)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)

    def OnBtnOneMonthButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Months(1)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)

    def OnBtnOneYearButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Years(1)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)

    def OnBtnAllDateButton(self, event):
        self.dateEnd.SetValue(wx.DateTime.Today())        
        self.dateStart.SetValue(wx.DateTimeFromDMY(1,0,2000,0,0,00))

    def OnBtnDayBeforeButton(self, event):
        day = self.dateEnd.GetValue()        
        span = wx.DateSpan.Days(1)
        day.SubtractDS(span)
        self.dateEnd.SetValue(day)
        self.dateStart.SetValue(day)

    def OnBtnDayAfterButton(self, event):
        day = self.dateEnd.GetValue()        
        span = wx.DateSpan.Days(1)
        day.AddDS(span)
        self.dateEnd.SetValue(day)
        self.dateStart.SetValue(day)

    def OnBtnSearchButton(self, event):
        self.refreshProductList()

    def OnTreeSelChanged(self, event):
        self.curCategory = self.categoryList.getSelection()
        self.refreshProductList()

        self.m_sku=((),())
        self.panelSku.set_category(self.curCategory.getKeyVal())       


    def refreshProductList(self):

        categoryIds = []
        if (self.curCategory is not None):
            categoryId = self.curCategory.getFieldVal(index=CDataCategory.COL_ID)
            categoryIds = CDataCategory.getChildId(categoryId)
        
        keywordIds = []

        self.proList.setFilter(keywordIds, categoryIds, self.m_sku)
        
        filter = {'dateStart':self.dateStart.GetValue().Format('%Y-%m-%d'),
                    'dateEnd':self.dateEnd.GetValue().Format('%Y-%m-%d'),
                    'mall_40':self.checkBox_40.GetValue(),
                    'mall_41':self.checkBox_41.GetValue()}

        self.proList.refresh(filter)

    def OnCheckBox_40Checkbox(self, event):
        self.refreshProductList()

    def OnCheckBox_41Checkbox(self, event):
        self.refreshProductList()

    def ChangeSkuSearch(self, sku):
        self.m_sku = sku
        print sku
        self.refreshProductList()

